package com.example.evaluate.service;

import com.example.evaluate.mapper.*;
import com.example.evaluate.model.Question;
import com.example.evaluate.model.Student;
import com.example.evaluate.model.Teacher;
import com.example.evaluate.model.TeacherAndQuestion;
import com.example.evaluate.model.TeacherAndStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService implements IStudentService {
    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StudentTeacherMapper studentTeacherMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    TeacherQuestionMapper teacherQuestionMapper;

    /**
     * 增加一名学生
     *
     * 增加一名学生需要增加三处记录，，学生表 teacher表，学生teacher表
     *
     * @param student  学生基本信息
     * @param teachers 学生对应的课程信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addStudent(Student student, List<Teacher> teachers) {
        // 添加学生
        if(studentMapper.addStudent(student)<=0)
            return false;
        int studentId = student.getStudentId();

        //循环遍历班级老师列表，添加到teacher表中 返回主键并添加到student_teacher中
        for (Teacher t: teachers) {
            Teacher temp = t;
            teacherMapper.addTeacher(temp);
            System.out.println("teacher的返回id："+temp.getTeacherId());

            TeacherAndStudent teacherAndStudent = new TeacherAndStudent(studentId,temp.getTeacherId());

            int res = studentTeacherMapper.addStudentTeacher(teacherAndStudent);
            if (res<0)
                return false;
        }

        return true;
    }

    /**
     * 删除一个学生
     * 同时要删除 question,student,等等等表
     *
     * @param studentId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteStudent(int studentId) {
        //删除question表
        questionMapper.deleteQuestionByStudentId(studentId);
        //删除teacher表
        teacherMapper.deleteTeacherByStudentId(studentId);
        //删除两个中间表
        studentTeacherMapper.deleteStudentTeacherByStudentId(studentId);
        teacherQuestionMapper.deleteTeacherQuestionByStudentId(studentId);
        //删除学生表
        return  studentMapper.deleteStudentByStudentId(studentId) > 0;

    }


    /**
     * 修改密码
     * @return 返回是否修改成功
     *
     * @param student 学生对象
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePassword(Student student) {
        int res = studentMapper.updatePassword(student);
        return res > 0;
    }

    /**
     * 登陆
     * @return 返回是否登陆成功
     *
     * @param student 学生对象
     */
    @Override
    public boolean login(Student student) {
        Student res = studentMapper.findStudentByStudentId(student.getStudentId());
        if (res==null)  //若为空 则查无此人
            return false;

        //不为空  看密码是否匹配
        if (res.getStudentPassword().equals(student.getStudentPassword()))
            return true;
        else
            return false;
    }

    /**
     * 查看学生信息
     *
     * @param student
     */
    @Override
    public Student findStudentByStudentId(Student student) {
        return studentMapper.findStudentByStudentId(student.getStudentId());
    }

    /**
     * 通过学生找学生对应老师列表
     *
     * @param student
     */
    @Override
    public List<Teacher> findTeacherByStudentId(Student student) {
        return studentTeacherMapper.findTeacherByStudentId(student.getStudentId());
    }

    /**
     * 通过学生找学生对应未评教的老师列表
     *
     * @param student
     */
    @Override
    public List<Teacher> findTeacherByStudentId2(Student student) {
        return studentTeacherMapper.findTeacherByStudentId2(student.getStudentId());
    }

    /**
     * 学生提交问卷
     *
     * @param teacherId
     * @param question
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean studentSubmitEvaluation(int studentId, int teacherId, Question question) {

        //先提交问题获取问题id
        Question question1 = question;
        questionMapper.addQuestion(question1);

        System.out.println("问题的id"+question1.getQuestionId());
        TeacherAndQuestion teacherAndQuestion = new TeacherAndQuestion(studentId,teacherId,question1.getQuestionId());

        System.out.println("输出检查："+ teacherAndQuestion.toString());

        int res = teacherQuestionMapper.addTeacherQuestion(teacherAndQuestion);
        return res > 0;
    }

    /**
     * 通过参数找老师
     *
     * @param studentId
     * @param param
     */
    @Override
    public List<Teacher> findTeacherByParam(int studentId, String param) {
        ArrayList<Teacher> t1 = (ArrayList<Teacher>) studentTeacherMapper.findTeacherByParams1(studentId,param);
        ArrayList<Teacher> t2 = (ArrayList<Teacher>) studentTeacherMapper.findTeacherByParams2(studentId,param);

        //进行查重
        List temp=new ArrayList(t1);//用来保存两者共同有的数据
        temp.retainAll(t2);//temp中只保留两者共同的数据
        t1.removeAll(temp);//l1中去掉两者共同有的数据
        List t3=new ArrayList();
        t3.addAll(t1);
        t3.addAll(t2);

        return t3;
    }


}
